package com.shmilyou.dissertationmanagement.conf;
/* Created with 岂止是一丝涟漪     530060499@qq.com    2018/11/23 */

import com.shmilyou.dissertationmanagement.interceptor.LoginInterceptor;
import com.shmilyou.dissertationmanagement.resolver.LoginUserArgumentResolver;
import com.shmilyou.dissertationmanagement.utils.Constant;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class SpringWebMvcConfigurer implements WebMvcConfigurer {

    // @Bean
    // public CommonsMultipartResolver commonsMultipartResolver() {
    //         final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
    //         commonsMultipartResolver.setMaxUploadSize(-1);
    //         return commonsMultipartResolver;
    // }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/static/**");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new LoginUserArgumentResolver());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/upload-attachments/theses/**").addResourceLocations("file:" + Constant.THESIS_ATTACHMENTS_PATH);
        registry.addResourceHandler("/upload-attachments/patents/**").addResourceLocations("file:" + Constant.PATENT_ATTACHMENTS_PATH);
        registry.addResourceHandler("/upload-attachments/softwareCopyrights/**").addResourceLocations("file:" + Constant.SOFTWARECOPYRIGHT_ATTACHMENTS_PATH);
    }


}
